<template>
  <div class="fillcontain">
    <head-top></head-top>
    <div class="table_container">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <!-- 具体内容，下拉框 row是下拉框中的对象，包含每个属性的对象id，address-->
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="活动 ID">
                <span>{{ props.row.id }}</span>
              </el-form-item>
              <el-form-item label="活动名称">
                <span>{{ props.row.name }}</span>
              </el-form-item>
              <el-form-item label="活动描述">
                <span>{{ props.row.des }}</span>
              </el-form-item>
              <el-form-item label="活动规则">
                <span>{{ props.row.ruler }}</span>
              </el-form-item>
              <el-form-item label="开始时间">
                <span>{{ props.row.startDate }}</span>
              </el-form-item>
              <el-form-item label="结束时间">
                <span>{{ props.row.endDate }}</span>
              </el-form-item>
              <el-form-item label="发布人">
                <span>{{ props.row.nickname }}</span>
              </el-form-item>

              <el-form-item label="发布时间">
                <span>{{ props.row.publicTime }}</span>
              </el-form-item>

               <el-form-item label="封面">
                <span>  <img v-if="props.row.cover" ref="img"
                            style="width: 100px; height: 100px"
                            :src="props.row.cover"
                         fit="fit"></img> </span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <!-- table head -->
        <el-table-column label="活动名称" prop="name"> </el-table-column>
        <el-table-column label="活动描述" prop="des"> </el-table-column>
        <el-table-column label="状态" prop="state"> </el-table-column>
        <el-table-column label="发布时间" prop="publicTime"> </el-table-column>
        <el-table-column label="操作" width="300">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="Success"
              @click="findActivityWork(scope.$index, scope.row)"
              >查看作品</el-button
            >
                <el-button
              size="mini"
              type="Success"
              @click="add(scope.$index, scope.row)"
              >添加</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="Pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="20"
          layout="total, prev, pager, next"
          :total="count"
        >
        </el-pagination>
      </div>
      <!-- 修改提示框 -->
      <el-dialog title="修改活动信息" v-model="dialogFormVisible">
        <div slot="footer">
          <el-form :model="selectTable">
            <el-form-item label="活动名称" label-width="100px">
              <el-input
                v-model="selectTable.name"
                auto-complete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="活动介绍" label-width="100px">
              <el-input v-model="selectTable.des"></el-input>
            </el-form-item>
            <el-form-item label="活动规则" label-width="100px">
              <el-input v-model="selectTable.ruler"></el-input>
            </el-form-item>
            <el-form-item label="开始结束日期" label-width="120px">
              <el-date-picker
                v-model="selectTable.startDate"
                type="date"
                placeholder="开始日期"
                :picker-options="pickerOptions6"
              >
              </el-date-picker>
              <el-date-picker
                v-model="selectTable.endDate"
                type="date"
                placeholder="结束日期"
                :picker-options="pickerOptions7"
              >
              </el-date-picker>
            </el-form-item>

            <el-form-item label="封面" label-width="100px">
                <el-row  >    
                    <el-col 
                     :span="3"  >             
                            <img v-if="selectTable.cover" ref="img"
                            style="width: 100px; height: 100px"
                            :src="selectTable.cover"
                            fit="fit"></img> 
                    </el-col>                
                </el-row>
                 <el-row type="flex" >    
                    <el-col :span="3">  
                             <el-upload class="avatar-uploader" action="http://localhost:8090/tourism/activity/updateCover" 
                                :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload"
                                 :data="{id:selectTable.id}"
                                :show-file-list="false"
                            >           
                           <el-button type="primary" >更改封面</el-button>
                             </el-upload>
                    </el-col> 
                </el-row>
                 
            </el-form-item>

          </el-form>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="update">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import headTop from "../components/headTop";
import { baseUrl, baseImgPath } from "@/config/env";

import {
  findActivityWorkByAcId,
  getActivitys,
  getActivitysByMethod,
  updateActivity,
  deleteActivityById
} from "@/api/activity";

export default {
  data() {
    return {
      baseUrl,
      baseImgPath,
      city: {},
      offset: 0,
      limit: 20,
      count: 0,
      tableData: [],
      currentPage: 1,
      selectTable: {},
      dialogFormVisible: false,
       pickerOptions6: {
      //结束时间不能大于开始时间
      disabledDate: (time) => {
        if (this.selectTable.endDate) {
          return time.getTime() > new Date(this.selectTable.endDate).getTime();
        } else {
          //还没有选择结束时间的时候，让他只能选择今天之后的时间包括今天
          return time.getTime() < Date.now() - 8.64e7;
        }
      },
    },
    pickerOptions7: {
      disabledDate: (time) => {
        if (this.selectTable.startDate) {
          // - 1 * 24 * 60 * 60 * 1000//可以选择同一天
          return time.getTime() < new Date(this.selectTable.startDate).getTime();
        } else {
          //还没有选择结束时间的时候，让他只能选择今天之后的时间包括今天
          return time.getTime() < Date.now() - 8.64e7;
        }
      },
    },
    };
  },
  computed:{

  },
  created() {
    this.initData();
    console.log(11111);
  },
  components: {
    headTop,
  },
  //这里运用async/await 进行异步处理，保证BMap加载进来后才执行后面的操作
  async mounted() {},

  methods: {
    //初始化数据
    async initData() {
      try {
        this.initActivitys();
        // const countData = await getResturantsCount();

        // this.getResturants();
      } catch (err) {
        console.log("获取数据失败", err);
      }
    },
   

    async initActivitys() {
      const res = await getActivitys();

      this.tableData = [];
      if (res.code >= 0) {
        const activitys = res.data;
        console.log("activitys:", activitys);
        this.count = activitys.length;
        this.activitys = activitys;
        activitys.forEach((item) => {
          this.tableData.push(item);
        });
      }
    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.offset = (val - 1) * this.limit;
      this.initScenery();
    },
    async handleEdit(index, row) {
      //index 就是第几个，可以从scenery直接取
      console.log("handleEdit(index, row)", index, row);

      this.selectTable = row;
      console.log("scope.row", row);
      //   this.addr = ;
      this.dialogFormVisible = true;
    },
    //
    async findActivityWork(index, row) {
      const id = row.id;
      this.$router.push({path: `/activityWorkList?id=${id}`});
      
    },
    add(index, row) {
      this.$router.push({
        path: "addActivity",
        query: { restaurant_id: row.id },
      });
    },
    async handleDelete(index, row) {
      try {
        const res = await deleteActivityById(row.id);
        if (res.code >= 0) {
          this.$message({
            type: "success",
            message: "删除景点成功",
          });
          this.tableData.splice(index, 1);
          this.initActivitys();
        } else {
          throw new Error(res.message);
        }
      } catch (err) {
        this.$message({
          type: "error",
          message: err.message,
        });
        console.log("删除店铺失败");
      }
    },

    //更新景点
    async update() {
      try {
        //把address添加到selectable中
        // Object.assign(this.selectTable, this.addr);

        console.log("this.selectTable", this.selectTable);
        const res = await updateActivity(this.selectTable);
        if (res.code >= 0) {
          this.$message({
            type: "success",
            message: "更新活动信息成功",
          });

          this.$router.go(0);
        } else {
          this.$message({
            type: "error",
            message: res.message,
          });
        }
      } catch (err) {
        console.log("更新活动信息失败", err);
      }
      this.dialogFormVisible = false;
    },

     handleAvatarSuccess(res, file) {
      if(res.code>=0){
        let pics = this.selectTable.pics
        this.selectTable.cover = res.data.cover      
        this.$message({
          message: '添加成功',
          type:"success"
        });
      }else{
         this.$message({
          message: res.message,
          type:"fail"
        });
      }
        
        console.log(res);
      },
      beforeAvatarUpload(file) {
        
        const isJPG = file.type === 'image/jpeg'||file.type=='image/jpg'||file.type=='png';
        const isLt2M = file.size / 1024 / 1024 < 2;
 
        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG ,PNG,JPEG格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
  },
};
</script>

<style lang="less">
@import "../style/mixin";
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.table_container {
  padding: 20px;
}
.Pagination {
  display: flex;
  justify-content: flex-start;
  margin-top: 8px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #20a0ff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
.avatar {
  width: 120px;
  height: 120px;
  display: block;
}
</style>
